#include "threadPool.h"
//  <unistd.h>
// 如果access报错要包含这个头文件
// 处理cd命令，即更新路径名
int cd_work(client_info_t *client_info)
{
    int netfd = client_info->netfd;
    // 先接收路径
    train_t train;
    memset(&train, 0, sizeof(train));
    recv(netfd, &train.len, sizeof(int), MSG_WAITALL);
    recv(netfd, train.data, train.len, MSG_WAITALL);

    if (strcmp(train.data, ".") == 0 || strcmp(train.data, "./") == 0)
    {
        // 当前目录不做操作
        printf("逻辑路径：%s\n", client_info->virtual_global_path);
        printf("真实路径：%s\n", client_info->global_path);
    }
    else
    {
        if (strcmp(train.data, "..") == 0 || strncmp(train.data, "../", 3) == 0)
        {
            // 退回上级目录
            // 找到当前路径中第一个斜杠的位置
            char *ptr = client_info->virtual_global_path;
            char *second_last_slash = NULL;
            char *last_slash = NULL;
            while (*ptr != '\0')
            {
                if (*ptr == '/')
                {
                    second_last_slash = last_slash;
                    last_slash = ptr;
                }
                ptr++;
            }
            if (second_last_slash != NULL)
            {
                // 将倒数第二个斜杠之后的部分截断
                *(second_last_slash + 1) = '\0';
            }

            // 同样地，也更新真实路径
            ptr = client_info->global_path;
            second_last_slash = NULL;
            last_slash = NULL;
            while (*ptr != '\0')
            {
                if (*ptr == '/')
                {
                    second_last_slash = last_slash;
                    last_slash = ptr;
                }
                ptr++;
            }
            if (second_last_slash != NULL)
            {
                // 将倒数第二个斜杠之后的部分截断
                *(second_last_slash + 1) = '\0';
            }
            printf("逻辑路径：%s\n", client_info->virtual_global_path);
            printf("真实路径：%s\n", client_info->global_path);
        }
        else
        {
            char *path = train.data;
            // 匹配./
            if (strncmp(path, "./", 2) == 0)
            {
                // 如果是./+路径,就跳过./
                path += 2;
            }
            // 更新客户端的虚拟路径和真实路径
            strcat(client_info->virtual_global_path, path);
            strcat(client_info->virtual_global_path, "/");
            strcat(client_info->global_path, path);
            strcat(client_info->global_path, "/");
            if (access(client_info->global_path, F_OK == -1))
            {
                printf("路径不存在：%s\n", client_info->global_path);
                bzero(&train, sizeof(train));
                train.len = strlen("fail");
                strcpy(train.data, "fail");
                send(netfd, &train, sizeof(train.len) + train.len, MSG_NOSIGNAL);
                return -1; // 返回错误码
            }
            // 测试代码
            printf("逻辑路径：%s\n", client_info->virtual_global_path);
            printf("真实路径：%s\n", client_info->global_path);
        }
    }
    bzero(&train, sizeof(train));
    train.len = strlen("success");
    strcpy(train.data, "success");
    send(netfd, &train, sizeof(train.len) + train.len, MSG_NOSIGNAL);

    return 0;
}